﻿using Prism.Events;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UI.Application.Login.Models;
using UI.Application.Share.Event;
using UI.Application.Share.Initialize;
using UI.Application.Share.Login;
using UI.Application.Share.Model;

namespace UI.Application.Login.ViewModels
{
    internal class LoginViewModel : ReactiveObject
    {
        private IEventAggregator EventAggregator { get; }

        public string Title { get; }
        public string Version => System.Windows.Application.ResourceAssembly.GetName().Version.ToString();
        [Reactive]
        public CurrentUser CurrentUser { get; set; } = new CurrentUser() { UserName = "admin", Password = "12345678" };
        public ICommand LoginCommand { get; }
        private ISession Session { get;  }
        public LoginViewModel(IEventAggregator eventAggregator,ISession session)
        {
            EventAggregator = eventAggregator;
            Session = session;
            LoginCommand = ReactiveCommand.Create(Login);
        }

        private void Login()
        {
            if (string.IsNullOrEmpty(CurrentUser.UserName) || string.IsNullOrEmpty(CurrentUser.Password))
            {
                return;
            }

            if (CurrentUser.UserName == "admin" && CurrentUser.Password == "12345678")
            {
                Session.CurrentUser = CurrentUser;
                EventAggregator.GetEvent<LoginSuccessEvent>().Publish(CurrentUser);  //通知用户登录成功
            }
        }
    }
}
